/*
 * $Id: sysapi_rmdir.c 50 2009-02-10 05:48:05Z hyunghwan.chung $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#include <xp/bas/sysapi.h>
#include <xp/bas/locale.h>
#include <xp/bas/errno.h>

#if defined(XP_USE_SYSCALL) && defined(XP_HAVE_SYS_SYSCALL_H)
#include <sys/syscall.h>
#endif

int xp_rmdir (const xp_char_t* path)
{
#ifdef _WIN32
	if (RemoveDirectory(path) == 0) {
		xp_seterrno (xp_maperrno(GetLastError()));
		return -1;
	}
	return 0;
#else

#ifdef XP_CHAR_IS_MCHAR
	#ifdef SYS_rmdir
	return syscall (SYS_rmdir, path);
	#else
	return rmdir (path);
	#endif
#else
	xp_mchar_t path_mb[XP_PATH_MAX + 1];
	if (xp_wcstomcs_strict(path, path_mb, xp_countof(path_mb)) == -1) {
		return -1;
	}
	#ifdef SYS_rmdir
	return syscall (SYS_rmdir, path_mb);
	#else
	return rmdir (path_mb);
	#endif
#endif

#endif
}
